package ch.zbw.sysventory.server.repository.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import ch.zbw.sysventory.server.model.User;
import ch.zbw.sysventory.server.repository.UserRepositoryCustom;

public class UserRepositoryCustomImpl implements UserRepositoryCustom {

	@PersistenceContext
	private EntityManager entityManager;

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	@Override
	public User getUserByLoginName(String loginName) {
		TypedQuery<User> query = this.entityManager.createQuery("select u from User u where u.loginName = :loginName", User.class);
		List<User> users = query.setParameter("loginName", loginName).getResultList();
		return users.size() == 1 ? users.get(0) : null;
	}

}
